PATHMac OS 8 and 9 Developer Documentation > Interapplication Communication > AppleScript for Scripters >

AppleScript Language Guide

   

String

A value of class String is a character string (an ordered series of characters) in AppleScript. For information on additional string value class types, see Unicode Text and International Text.

LITERAL EXPRESSIONS

Strings in scripts are always surrounded by quotation marks, as in these examples:

"string"
"Rolling along, stringing a song"
"Pennsylvania 68000"

To include quotation marks in a string, you must use the two-character sequence, \" . For more information, see "Special Characters in Strings" later in this section.

PROPERTIES
Class
The class identifier for the object. This property is read-only, and its value is always string .
Length
The number of characters in the string.
ELEMENTS

Strings can have character, word, paragraph, and text elements.

The elements of a string may be different from the character, word, paragraph, and text objects of applications.

Character
A single character contained in the string.
Paragraph
A series of characters beginning immediately after either the first character after the end of the preceding paragraph or the beginning of the string and ending with either a return character or the end of the string.
Text
A continuous series of characters, including spaces, tabs, and all other characters, within a string (see "Notes" later in this section).
Word
A continuous series of characters that contains only the following types of characters:
letters (including letters with diacritical marks) digits nonbreaking spaces dollar signs, cent signs, English pound symbols, or yen symbols percent signs commas between digits periods before digits apostrophes between letters or digits
hyphens (but not minus signs [Option-hyphen] or dashes [Option-Shift-hyphen]).
Here are some examples of words:
read-only he's v1.0 $99.99 12c-d
This definition of a word applies to English text in the Roman script system. Words in other languages are defined by the script system for each language if the appropriate script system is installed.
OPERATORS

The operators that can have strings as operands are & , = , , > , , < , , Starts With, Ends With, Contains, Is Contained By, and As.

For detailed explanations and examples of how AppleScript operators treat strings, see Operators That Handle Operands of Various Classes.

REFERENCE FORMS

You can use the following reference forms to refer to elements of strings:

You cannot use the Relative, Name, ID, or Filter reference forms.

SPECIAL CHARACTERS IN STRINGS

The backslash ( \ ) and double-quote ( " ) characters have special meaning in strings. If you want to include either of these characters in a string, you must use the equivalent two-character sequence:

Backslash character \\
Double-quote character \ "

The tab and return characters can be included in strings, or they can be represented by equivalent two-character sequences:

Tab character \t
Return character \r

When a string containing any of the two-character sequences is displayed to the user (as, for example, in a dialog box), the sequences are converted. For example, the string

"item 1\t1\ritem 2\t2"

is displayed in a dialog box as

item 1      1
item 2      2
STRING CONSTANTS

AppleScript defines three constants for string values:

Constant

Value

space " "
tab "\t"
return "\r"
COERCIONS SUPPORTED

If a string represents an appropriate number, AppleScript supports coercion of the string to an integer, a number, or a real number. Similarly, any integer, number, or real number can be coerced to a string. AppleScript also supports coercion of a string to a single-item list and coercion of a list whose items can all be coerced to strings to a single concatenated string. Starting with version 1.3.7, AppleScript supports coercion of a constant, such as Monday or January, to a string.

NOTES

There is no limit on the length of strings except the memory available in the computer.

To get a contiguous range of characters within a string, use the text element. For example, the value of the following statement is the string "y thi" .

get text 3 thru 7 of "Try this at home"
--result: "y thi"

The result of a similar statement using the character element instead of the text element is a list.

get characters 3 thru 7 of "Try this at home"
--result: {"y", " ", "t", "h", "i"}

You cannot set the value of an element of a string. For example, if you attempt to change the value of the first character of the string "Boris" as shown in the following example, you'll get an error.

set myName to "Boris"
set character 1 of myName to "D"
--results in an error, because you cannot set the values of
--elements of strings

However, you can modify the name by getting the last four characters and concatenating them with "D":

set myName to "boris"
set myName to "D" & (get text 2 through 5 of myName)
--result: "Doris"

© 1999 Apple Computer, Inc. – (Last Updated 21 May 99)